import Core from '@/core.js'
import recom from '@/components/regionComponent'

export default {
  components: {
    recom
  },
  data() {
    return {
      Core,
      dataList: [{
        name: '配置',
        checked: false,
        normalIcon: Core.globalImgUrlBussiness + '/newpage/R_lookingCar/xia@2x(1).png',
        activeIcon: Core.globalImgUrlBussiness + '/newpage/R_lookingCar/xia@2x.png',
      },
      {
        name: '外观/内饰',
        checked: false,
        normalIcon: Core.globalImgUrlBussiness + '/newpage/R_lookingCar/xia@2x(1).png',
        activeIcon: Core.globalImgUrlBussiness + '/newpage/R_lookingCar/xia@2x.png',
      },
      {
        name: '地区',
        checked: false,
        normalIcon: Core.globalImgUrlBussiness + '/newpage/R_lookingCar/xia@2x(1).png',
        activeIcon: Core.globalImgUrlBussiness + '/newpage/R_lookingCar/xia@2x.png',
      },
      {
        name: '筛选',
        checked: false,
        normalIcon: Core.globalImgUrlBussiness + '/newpage/R_lookingCar/xia@2x(1).png',
        activeIcon: Core.globalImgUrlBussiness + '/newpage/R_lookingCar/xia@2x.png',
      }
      ],

      typeList: [{
        name: '不限',
        checked: true,
      },
      {
        name: '车源商',
        checked: false,
      },
      {
        name: '4S',
        checked: false,
      },
      {
        name: '汽贸',
        checked: false,
      },
      {
        name: '直营店',
        checked: false,
      }
      ],
      paiList: [{
        name: '智能排序',
        checked: true,
      },
      {
        name: '价格排序',
        checked: false,
      }
      ],

      TypeShow: false, //类型弹窗显示隐藏
      TypeSelector: ['车源商', '汽贸店', '4S店', '直营店'], //类型弹窗列表
      BrandShow: false, //品牌弹窗显示印隐藏
      brandlist: [], //品牌弹窗列表
      zoneShow: false, //地区显示隐藏
      bid: '', // 品牌id
      regsion: '', // 区县
      storetype: '', // 车商类型
      searchkey: '', // 搜索关键字
      CurrentPage: 1, // 当前页
      showimg: false, //默认图片

      busCarList: [], //列表
      idlistid: '', //配置查询车子id
      idlistyear: '', //配置查询年份/全部
      cartypeid: '', //系列id(根据系列id查询或者根据搜索查询)
      colourname: '', // 颜色名称（
      district: '', //地区名称
      storetype: 0, //店铺类型（0全部，1车源店，2汽贸店，3 4S店，4直营店） ,
      rank: 0, //排序默认0 ，价格1 ,
      CarList: [], //配置列表
      colorList: [], //颜色列表
      carDateList: [], //年份列表
      peiShow: false, //选择的筛选条件名称 --配置
      seShow: false, //选择的筛选条件名称 --外观/内饰
      zoneShow: false, //选择的筛选条件名称 --地区
      chooseShow: false, //选择的筛选条件名称 --筛选
      cityList: [], //城市列表
      allYear: false, //是否选的是全部年份
      toView: '',
      liststr: [],
    }
  },
  onLoad(e) {
    if (e.searchkey !== undefined) {
      this.searchkey = e.searchkey
    }
    // this.getList(1) //寻车列表
    // this.getBrandList()
  },
  onUnload(){
	    uni.$off('year')  //在此生命周期里销毁地址改变事件的监听
  },
  methods: {
    //点击弹出当前弹窗，关闭其他弹窗
    ChooseSelector(name, index) {
      if (index === 0) { //配置
        this.peiShow = !this.peiShow //显示当前弹窗
        this.seShow = false //外观/内饰弹窗关闭
        this.zoneShow = false //地区弹窗关闭
        this.chooseShow = false //筛选弹窗关闭
        uni.$on('year',data => { //如果上次选了的，再次打开页面时复显出来
          this.idlistyear=data[0]
          this.idlistid=data[1]
        })
      } else if (index === 1) { //外观/内饰
        this.seShow = !this.seShow
        this.peiShow = false //显示当前弹窗
        this.zoneShow = false //地区弹窗关闭
        this.chooseShow = false //筛选弹窗关闭
      } else if (index === 2) { //地区
        this.zoneShow = !this.zoneShow //地区弹窗关闭
        this.seShow = false
        this.peiShow = false //显示当前弹窗
        this.chooseShow = false //筛选弹窗关闭
        this.getBrandList() //城市列表
      } else { //筛选
        this.chooseShow = !this.chooseShow //筛选弹窗关闭
        this.seShow = false
        this.peiShow = false //显示当前弹窗
        this.zoneShow = false //地区弹窗关闭
      }
    },
    //标题样式停留
    changeClass(index) {
      // for (var i = 0; i < this.dataList.length; i++) {
      // 	this.dataList[i].checked = false
      // }
      this.dataList[index].checked = true
    },
    // 清除当前样式停留
    clearClass(index) {
      this.dataList[index].checked = false
    },
    //点击搜索
    search() {
      this.getList(1)
    },
    // 车源列表
    getList(type) {
      if (type === 1) { //寻车列表初始化
        this.CurrentPage = 1
      } else { //寻车列表翻页
        this.CurrentPage += 1
      }
      const formdata = {
        idlistid: this.idlistid, // (string, optional): 配置查询车子id（不传就传null） ,
        idlistyear: this.idlistyear, // (string, optional): 配置查询年份/全部（不传就传null） ,
        searchkey: this.searchkey, // (string, optional): 搜索关键字(根据系列id查询或者根据搜索查询) ,
        cartypeid: this.cartypeid, // (string, optional): 系列id(根据系列id查询或者根据搜索查询) ,
        colourname: this.colourname, // (string, optional): 颜色名称（全部颜色传null） ,
        district: this.district, // (string, optional): 地区名称 ,
        storetype: this.storetype, // (integer, optional): 店铺类型（0全部，1车源店，2汽贸店，3 4S店，4直营店） ,
        rank: this.rank, // (integer, optional): 排序默认0 ，价格1 ,
        CurrentPage: this.CurrentPage, // (integer, optional): 当前页 ,
        PageSize: 10, // (integer, optional): 每页多少条
      }
      Core.AjaxPost(Core.Bus.busNewCarGoodsListApiAddress, formdata, res => {
        if (type === 1) {
          if (res.data.Item.busCarList.length === 0) { //首次加载没有数据
            this.showimg = true
          } else {
            this.showimg = false
          }
          this.busCarList = res.data.Item.busCarList.List //车子列表
          this.CarList = res.data.Item.CarList //配置列表
          this.colorList = res.data.Item.colorList //颜色列表
          this.carDateList = res.data.Item.carDateList //年份列表
        } else {
          if (res.data.Item.busCarList === '') { //翻页到底
            this.moreInfo = '已加载全部数据'
            this.moretype = 2
          }
          this.busCarList = this.busCarList.concat(res.data.Item.busCarList.List)
        }
      })
    },
    //筛选弹窗----功能按钮
    chooseSub(index) { //index 0 清空 1 确定
      if (index === 0) { //index 0 清空
        for (var i = 0; i < this.typeList.length; i++) {
          this.typeList[i].checked = false
        }
        for (var i = 0; i < this.paiList.length; i++) {
          this.paiList[i].checked = false
        }
        this.typeList[0].checked = true
        this.paiList[0].checked = true
        this.clearClass(3)//清除标题停留样式
      } else { //index 1 确定
        this.getList(1) //调接口
        this.chooseShow = false //关闭弹框
      }
    },

    //筛选弹窗---样式停留
    changeChooseClass(type, index) { //type 0 商家类型 1排序  index 当前选的哪个   storetype  rank
      var list = []
      if (type === 0) { //type 0 商家类型
        list = this.typeList
        this.storetype = index //店铺类型
      } else { // 1排序
        list = this.paiList
        this.rank = index
      }
      for (var i = 0; i < list.length; i++) {
        list[i].checked = false
      }
      list[index].checked = true
      this.changeClass(3) //标题样式停留
    },
    //一级城市列表
    getBrandList() {
      const formdata = {
        searchkey: '', //  搜索关键字
        level: 2 //  1、省份 2、市级  3、区县
      }
      Core.AjaxPost(Core.Bus.GetAllCityApiAddress, formdata, res => {
        this.cityList = res.data.Item.list
        this.liststr = res.data.Item.liststr
      })
    },
    //选择城市
    chooseCity(name) {
      this.district = name
      this.zoneShow = false //关闭弹窗
      this.dataList[2].name = name
      this.changeClass(2) //标题样式停留
      this.getList(1) //调接口
    },
    //配置---选择年份
    changeYear(index, data) {
      if (data === '全部') {
        this.idlistyear = ''
        this.allYear = true //是否选的是全部年份
        for (var i = 0; i < this.carDateList.length; i++) {
          this.carDateList[i].active = false
        }
      } else {
        this.idlistyear = data
        this.allYear = false //是否选的是全部年份
        //添加移除样式
        for (var i = 0; i < this.carDateList.length; i++) {
          this.carDateList[i].active = false
        }
        this.carDateList[index].active = true
      }
      this.changeClass(0) //标题样式停留
    },
    //配置---选择车系列表
    choosePei(pindex, index, id) {
      for (var i = 0; i < this.CarList.length; i++) { //选中的列表清空
        for (var q = 0; q < this.CarList[i].list.length; q++) {
          this.CarList[i].list[q].active = false
        }
      }
      this.CarList[pindex].list[index].active = true
      this.idlistid = id
      this.changeClass(0) //标题样式停留
    },
    //配置---点击清空确定按钮
    choosePeiSub(type) {
      if (type === 0) { //清空
        for (var i = 0; i < this.CarList.length; i++) { //选中的列表清空
          for (var q = 0; q < this.CarList[i].list.length; q++) {
            this.CarList[i].list[q].active = false
          }
        }
        this.allYear = false //年份的全部清除
        for (var a = 0; a < this.carDateList.length; a++) { //年份清除
          this.carDateList[a].active = false
        }
        this.clearClass(0)//清除标题停留样式
      } else { //确定
        this.peiShow = false //关闭弹窗
        this.getList(1) //调接口
        // 将选中的配置存下来 离开页面前，下次打开弹窗的时候还有数据  idlistid  idlistyear
        uni.$emit('year',[
          this.idlistyear,this.idlistid
        ])
      }
    },
    // 外观内饰--选择
    chooseColor(name) {
      if (name === '色全') {
        this.colourname = ''
      } else {
        this.colourname = name
      }
      this.seShow = false //关闭外观内饰弹窗
      this.getList(1) //调接口
      this.changeClass(1) //标题样式停留
    },
    //跳转详情页
    goDetail(goodsid) {
      uni.navigateTo({
        url: '../../pages_Sxy/Sxy_carSourceDetail/Sxy_carSourceDetail?goodsid=' + goodsid
      })
    },
    // 楼层定位
    bindToView(event) {
      var id = event.currentTarget.dataset.id
      console.log(id)
      this.toView = id
    },
    //点击弹窗黑色背景部分，关闭所有弹窗
    closePoup() {
      this.peiShow = false
      this.seShow = false
      this.zoneShow = false
      this.chooseShow = false
    },
  },
  onReachBottom: function() {
    this.getList(2)
  }
}
